home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 4586 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: walt.tsc.com!not-for-mail
  2. From: billr@elmer.tsc.com (Bill Roberts)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: x ^= y ^= x ^= y;
  5. Date: 1 Mar 1996 12:22:45 -0500
  6. Organization: Technology Service Corporation
  7. Sender: Bill Roberts
  8. Message-ID: <4h7bp5$mf@elmer.tsc.com>
  9. References: <1286.6624T1439T237@cs.ruu.nl> <3131C523.1ECC@sapiens.com> <4h075l$2go@elmer.tsc.com> <2141.6632T1213T2220@cs.ruu.nl>
  10. NNTP-Posting-Host: elmer.tsc.com
  11.  
  12. In article <2141.6632T1213T2220@cs.ruu.nl>,
  13. Wessel Dankers <wsldanke@cs.ruu.nl> wrote:
  14. !Bill Roberts <billr@elmer.tsc.com> wrote:
  15. !> What expression result will be in x at the end of the expression?  The
  16. !> first one or the second one?  The compiler may choose either.  The c
  17. !No it definitely CAN'T choose!
  18. !The return value of an assignment is strictly documented and an ANSI feature!
  19. !
  20. But which experssion is stored, the first one calculated or the
  21. second?  The C standard specifically says that the compiler may
  22. optimize the statement as it chooses.  This problem is specifically
  23. discussed in the comp.lang.c FAQ's.  The results are undefined.  Your
  24. compiler may give you the result you expect.  Another compiler may not.
  25. !The order is predefined: an assignment statement is of course right-
  26. !associative:
  27. !(x = y) = 4
  28. !has no meaning: how can you assign a value to a statement? A statement is no
  29. !lvalue.
  30. !x = (y = 4)
  31. !however, _is_ sensible.
  32. !
  33. As is x = y =4.  However x += x += 4 is not.  You have assigned two
  34. different values to x.  You expect that 4 will be added to the current
  35. value of x, then that new value will replace the current value of x.
  36. Then the new value of x will be added to which value of x, the new
  37. value or the original value.  Both are equally valid assumptions.
  38. Which did you want?  Which did the compiler writer choose?  
  39. x += ( x += 4 ); doesn't change the question.
  40.  
  41. !> problem you will have with
  42. !>    i = 1;
  43. !>    x[ i++ ] = y[ i++ ];
  44. !> What will be the value of i at the end?  And which value will be used
  45. !i++ means: increase i with one after using it. In this expression i is
  46. !increased twice in each pass.
  47. !while(d++ = s++);
  48. !for two arrays d[] and s[] (for each type) is valid, though.
  49. !       
  50. Not the same problem.  x++ is not the same as x[i++].
  51.  
  52.